/**
 * Created by Administrator on 2016/11/18.
 */
class TouchBehaviour implements IGameTapEvent {

    private static _i: TouchBehaviour;
    static get i() {
        return TouchBehaviour._i || (TouchBehaviour._i = new TouchBehaviour);
    }

    onBGTap() {
        GSController.i.moveBack();
        GSController.i.hideAngleHandlePanel();
    }

    onFuncSelect(funcIndex: number): void {
        let obj = GSData.i.funcSelects[funcIndex];
        switch (obj.index) {
            case 0://过
                GSData.i.isShowFunc = false;
                gameManager.socketManager.send(15, { action: obj.action, pai: [] });

                GSController.i.hideFuncSelectMenu();

                if (GSData.i.readyTing) {
                    GSData.i.readyTing = false;
                    GSController.i.cancleTingView();
                    GSController.i.activateCard = null;
                }
                if (obj.hasAction == 99 && GSData.i.handCardPrompt) {
                    GSController.i.showHandPaiParompt();
                }
                GSController.i.delayAutoPushPai();
                break;
            case 1://吃
                GSData.i.chiObj = obj;
                GSController.i.funcSelectAction = 1;
                //显示吃的提示框
                GSController.i.showChiSelectView();
                break;
            case 2://碰
                gameManager.socketManager.send(15, { action: obj.action, pai: obj.pai });
                GSController.i.hideFuncSelectMenu();
                break;
            case 3://杠
                GSController.i.funcSelectAction = 3;
                //显示杠的选择
                GSController.i.showGangSelectView();
                break;
            case 4://补蛋
                GSController.i.funcSelectAction = 4;
                GSController.i.showBudanSelectView();
                break;
            case 5://听
                console.log("听牌处理");
                GSController.i.hideFuncSelectMenu();
                GSController.i.doTing();
                GSController.i.showHandPaiParompt();
                break;
            case 6://胡牌
                gameManager.socketManager.send(15, { action: obj.action, pai: [] });
                GSController.i.hideFuncSelectMenu();
                break;
        }
    }

    //牌型选择
    onGroupSelect(index: number): void {
        if (GSController.i.funcSelectAction == 1) {//吃
            let funcSelect = GSData.i.getFuncSelectByIndex(1);
            let pais = [funcSelect.group[index].pai[0], funcSelect.group[index].pai[2]];
            gameManager.socketManager.send(15, { action: 1, pai: pais });
        }

        if (GSController.i.funcSelectAction == 3) {//杠
            let funcSelect = GSData.i.getFuncSelectByIndex(3);
            let pai = funcSelect.group[index].pai;
            let action = funcSelect.group[index].action;
            //发送消息
            gameManager.socketManager.send(15, { "action": action, "pai": pai });
            /**杠牌后取消听牌提示的自动打牌 */
            if (GSData.i.handTingCard !== null) {
                GSData.i.autoPushPai = false;
                gameManager.socketManager.send(41, { pos: PublicVal.i.getPos(1), flag: false });
            }
        }

        if (GSController.i.funcSelectAction == 4) {//补蛋
            let funcSelect = GSData.i.getFuncSelectByIndex(4);
            let pai = funcSelect.group[index].pai;
            let action = funcSelect.group[index].action;
            //发送消息
            gameManager.socketManager.send(15, { "action": action, "pai": [pai] });
        }

        GSController.i.hideFuncSelectMenu();
    }

    //继续游戏
    onContinue(): void {
        //清理结算界面内容
        GSController.i.closeResultView();
        if (GSController.i.jiesuanData) {
            //清理回合数据
            PublicVal.state = StateType.over;
            game.roomReady();
            GSController.i.showStateView();
            GSController.i.showTitleView(GSController.i.jiesuanData);
        } else {
            //清理回合数据
            GSData.i.roundReset();
            gameManager.socketManager.send(17, {});
        }
    }

    //设置
    onSettingTap() {
        gameManager.open('SettingPanel');
    }

    //退出
    onQuitTap() {
        this.onJiesanTap();
    }

    //打开聊天
    onTalkTap() {
        gameManager.open('ChatPanel');
    }

    //语音开始
    onSiriBegin(): void {
        Weixin.startRecord();
        GameOthers.showVoice();
    }

    //语音结束
    onSiriEnd(): void {
        Weixin.stopRecord();
        GameOthers.hideVoice();
    }

    //重连继续按钮
    onRebackContinue(e: egret.TouchEvent): void {
        e.currentTarget.visible = false;
        gameManager.socketManager.send(17, {});
    }

    //解散房间
    onJiesanTap(): void {
        //if (PublicVal.state == StateType.shuffle) {
        //    EffectTextUtils.showTips('牌局已经开始了');
        //    return;
        //}
        if (PublicVal.state == StateType.ready) {
            let info: string;
            if (game.roomPos == 1 && !game.isDaikai) {
                info = "牌局尚未开始，现在解散房间不扣房卡，\n确定要解散吗？";
            } else {
                info = "您确定要离开房间吗？";
            }
            if (game.roomState == RoomState.CIRCLE) {
                info = "您确定要离开房间吗？";
            }
            game.askPanel.showMsg(function (r) {
                if (r) {
                    gameManager.socketManager.send(12);   //离开房子
                }
            }, info);
        } else if (PublicVal.state == StateType.replay) {
            game.askPanel.showMsg(function (r) {
                if (r) {
                    Replayer.i.exit();
                }
            }, "您确定要退出回放吗？");
        }
        else {
            if (game.dissolution && game.dissolution.vote) {
                gameManager.open('DissolutionPanel');
            }
            else {
                game.askPanel.showMsg(function (r) {
                    if (r) {
                        gameManager.socketManager.send(14, { answer: 1 });//发起解散房子
                    }
                }, "您确定发起解散房间吗？\n（当所有在线玩家同意解散之后房间将解散）");
            }
        }
    }
    /**胡牌提示按钮 */
    onHutishiTap() {
        const pais = PublicVal.i.getHandPais(1);
        if (GSConfig.allowLens(pais.length)) { return }
        const ct = GSController.i;
        if (ct.scene.hcPanel.visible) {
            ct.hideAngleHandlePanel();
        } else {
            const pos: number = PublicVal.i.getPos(1);
            gameManager.socketManager.send(40, { pos: pos, pai: [] });
        }
    }

    //回到微信
    onWeixinTap(): void {
        Weixin.closeWindow();
    }

    //游戏开始
    onStartGame(): void {
        gameManager.socketManager.send(10, {});  //发送开始游戏
    }

    onReplayPlayTap(): void {
        Replayer.i.stop = false;
        GSController.i.gsView.replayMenu.play();
        GameSceneReplayMenu.play();
    }

    onReplayPauseTap(): void {
        Replayer.i.stop = true;
        GSController.i.gsView.replayMenu.pause();
        GameSceneReplayMenu.pause();
    }

    onReplayFFTap(): void {
        Replayer.i.FF();
        Replayer.i.stop = false;
        GSController.i.gsView.replayMenu.play();
        GameSceneReplayMenu.play();
    }

    onReplayFBTap(): void {
        Replayer.i.FB();
        Replayer.i.stop = false;
        GSController.i.gsView.replayMenu.play();
        GameSceneReplayMenu.play();
    }
}